package com.wkh.maker.generator.main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.wkh.maker.generator.JarGenerator;
import com.wkh.maker.generator.ScriptGenerator;
import com.wkh.maker.generator.file.DynamicFileGenerator;
import com.wkh.maker.meta.Meta;
import com.wkh.maker.meta.MetaManager;

import java.io.File;

/**
 * @author wangkehua
 * 模板
 */
public class GeneratorTemplate {
    public void doGenerator() {
        //1.把json转换成对象
        Meta metaObject = MetaManager.getMetaobject();
        System.out.println(metaObject);

        //2.输出根路径
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separatorChar + "generated" + File.separatorChar + metaObject.getName();
        doGenerator(metaObject,outputPath);
    }
    public void doGenerator(Meta meta,String outputPath) {
        //1.输出根路径
        if (!FileUtil.exist(outputPath)){
            FileUtil.mkdir(outputPath);
        }
        //2.复制原始文件的地址
        String sourceCopyDestPath = copySource(meta, outputPath);
        //3.生成代码
        generator(meta, outputPath);
        //4.构建jar包
        String jarPath = buildJar(outputPath, meta);

        //5.封装脚本
        String shellOutputFilePath = buildScript(outputPath, jarPath);

        //6.生成精简版的代码生成器
        builderDist(outputPath, shellOutputFilePath, jarPath,sourceCopyDestPath);
    }

    protected String builderDist(String outputPath, String shellOutputFilePath, String jarPath, String sourceCopyDestPath) {
        String distOutputPath = outputPath + "-dist";
        //拷贝jar包
        String targetAbsolutePath = distOutputPath + File.separatorChar + "target";
        //如果不存在就创建包路径
        if (!FileUtil.exist(targetAbsolutePath)){
            FileUtil.mkdir(targetAbsolutePath);
        }
        String jarAbsolutePath = outputPath + File.separatorChar + jarPath;
        //拷贝jar包
        FileUtil.copy(jarAbsolutePath,targetAbsolutePath,true);
        //拷贝脚本文件
        FileUtil.copy(shellOutputFilePath,distOutputPath,true);
        FileUtil.copy(shellOutputFilePath +".bat",distOutputPath,true);
        //拷贝源模板文件
        FileUtil.copy(sourceCopyDestPath,distOutputPath,true);
        return distOutputPath;
    }

    protected String buildScript(String outputPath, String jarPath) {
        String shellOutputFilePath = outputPath + File.separatorChar + "generator";
        ScriptGenerator.doGenerator(shellOutputFilePath,jarPath);
        return shellOutputFilePath;

    }

    protected String buildJar(String outputPath,Meta metaObject) {
        //构建jar包
        try {
            String jarName = String.format("%s-%s-jar-with-dependencies.jar", metaObject.getName(), metaObject.getVersion());
            String jarPath = "target" + File.separatorChar + jarName;
            JarGenerator.doGenerator(outputPath);
            return jarPath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 制作工具包
     * @param outputPath
     * @return 压缩包路径
     */
    protected String buildZip(String outputPath) {
        String zipPath = outputPath + ".zip";
        ZipUtil.zip(outputPath,zipPath);
        return zipPath;
    }

    protected void generator(Meta metaObject, String outputPath) {
        // 读取 resources 目录
        String inputResourcePath = "";

        // Java 包基础路径
        String outputBasePackage = metaObject.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputBaseJavaPackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;

        String inputFilePath;
        String outputFilePath;

        try {
            // model.DataModel
            inputFilePath = inputResourcePath + File.separator + "templates/java/model/DataModel.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/model/DataModel.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // cli.command.ConfigCommand
            inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/ConfigCommand.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/cli/command/ConfigCommand.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // cli.command.GenerateCommand
            inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/GeneratorCommand.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/cli/command/GeneratorCommand.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // cli.command.JsonGenerateCommand
            inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/JsonGeneratorCommand.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/cli/command/JsonGeneratorCommand.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // cli.command.ListCommand
            inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/ListCommand.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/cli/command/ListCommand.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // cli.CommandExecutor
            inputFilePath = inputResourcePath + File.separator + "templates/java/cli/CommandExecutor.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/cli/CommandExecutor.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // Main
            inputFilePath = inputResourcePath + File.separator + "templates/java/Main.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/Main.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // generator.DynamicGenerator
            inputFilePath = inputResourcePath + File.separator + "templates/java/generator/DynamicGenerator.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/generator/DynamicGenerator.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // generator.MainGenerator
            inputFilePath = inputResourcePath + File.separator + "templates/java/generator/MainGenerator.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/generator/MainGenerator.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // generator.StaticGenerator
            inputFilePath = inputResourcePath + File.separator + "templates/java/generator/StaticGenerator.java.ftl";
            outputFilePath = outputBaseJavaPackagePath + "/generator/StaticGenerator.java";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);

            // pom.xml
            inputFilePath = inputResourcePath + File.separator + "templates/pom.xml.ftl";
            outputFilePath = outputPath + File.separator + "pom.xml";
            DynamicFileGenerator.doGenerator(inputFilePath , outputFilePath, metaObject);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected String copySource(Meta metaObject, String outputPath) {
        //原始文件的地址
        String sourceRootPath = metaObject.getFileConfig().getSourceRootPath();
        String sourceCopyDestPath = outputPath + File.separatorChar + ".source";
        //拷贝文件
        FileUtil.copy(sourceRootPath,sourceCopyDestPath,false);
        return sourceCopyDestPath;
    }
}
